package com.aysf.drafter.controller;

import com.aysf.drafter.entity.CustomerCheckIn;
import com.aysf.drafter.entity.CustomerCheckOut;
import com.aysf.drafter.entity.CustomerOuting;
import com.aysf.drafter.entity.Result;
import com.aysf.drafter.service.ICustomerCheckInService;
import com.aysf.drafter.service.ICustomerCheckOutService;
import com.aysf.drafter.service.ICustomerOutingService;
import com.aysf.drafter.vo.CheckOutingVO;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author MaoZz
 * @since 2024-12-21 17:19:04
 */
@RestController
@CrossOrigin
@RequestMapping("/customerOuting")
public class CustomerOutingController {

    @Autowired
    private ICustomerOutingService customerOutingService;
    @Autowired
    private ICustomerCheckInService customerCheckInService;
    @Autowired
    private ICustomerCheckOutService customerCheckOutService;

    @RequestMapping("/getall")
    public Result getCustomerOuting(Integer current, Integer size,String key) {
        //三表连历
        Page<CustomerOuting> page = new Page<>(current, size);
        Wrapper<CustomerOuting> Wrapper =
                new QueryWrapper<CustomerOuting>()
                        .orderByDesc("customer_id");
        Page<CustomerOuting> page1 = customerOutingService.page(page, Wrapper);
        System.out.println(page1.getRecords().toString());
        List list = new ArrayList<CheckOutingVO>();
        for (CustomerOuting record : page1.getRecords()) {
            CheckOutingVO checkOutingVO = new CheckOutingVO();
            checkOutingVO.setTotal((int) page1.getTotal());
            CustomerCheckIn checkIn = customerCheckInService.getOne(new QueryWrapper<CustomerCheckIn>()
                    .eq("customer_id", record.getCustomerId())
                    .like("name",key));
            CustomerCheckOut one = customerCheckOutService.getOne(new QueryWrapper<CustomerCheckOut>()
                    .eq("customer_id", record.getCustomerId()));
            if(checkIn!=null){
                checkOutingVO.setName(checkIn.getName());
                checkOutingVO.setGender(checkIn.getGender());
                checkOutingVO.setAge(checkIn.getAge());
                checkOutingVO.setPhone(checkIn.getPhone());
                checkOutingVO.setCompanion(checkIn.getCompanion());
                checkOutingVO.setIdCard(checkIn.getIdCard());
            }
           if(one!=null){
               checkOutingVO.setStatus(one.getStatus());
               checkOutingVO.setReviewer(one.getReviewer());
               checkOutingVO.setReviewDate(one.getReviewDate());
               checkOutingVO.setApplicationDate(one.getApplicationDate());
           }
           checkOutingVO.setOutingDate(record.getOutingDate());
           checkOutingVO.setOutingReason(record.getOutingReason());
           checkOutingVO.setOutingDate(record.getOutingDate());
           checkOutingVO.setActualReturnDate(record.getActualReturnDate());
           checkOutingVO.setRemarks(record.getRemarks());
            list.add(checkOutingVO);
        }
        System.out.println(list);
        if (list.size() > 0) {
            return new Result("success", list);
        } else return new Result("error", null);
    }



    @RequestMapping("/register")
    public Result register(@RequestBody CheckOutingVO checkOutingVO) {
        LocalDateTime now = LocalDateTime.now();
        checkOutingVO.setActualReturnDate(LocalDate.from(now));
        CustomerCheckIn checkIn = customerCheckInService
                .getOne(new QueryWrapper<CustomerCheckIn>()
                        .eq("name", checkOutingVO.getName()));
        boolean update = customerOutingService.update(new UpdateWrapper<CustomerOuting>()
                .eq("customer_id", checkIn.getCustomerId())
                .set("actual_return_date",now));
        if(update)return  new Result("success", checkOutingVO);
        else return new Result("error", null);


    }


}
